/*
 * 铁路道岔故障检测信息管理系统
 * Design by 魏梓晟
 * Version :1.0
 */
package com.railway.switchs.service.impl.authority;

import com.railway.switchs.enums.EnumPermissionType;
import com.railway.switchs.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.railway.switchs.dao.authority.AuthorityPermissionDao;
import com.railway.switchs.domain.dto.authority.AuthorityPermission;
import com.railway.switchs.domain.query.authority.AuthorityPermissionQuery;
import com.railway.switchs.service.authority.IAuthorityPermissionService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * authority_permission - 资源表 Service实现类
 * @author 魏梓晟
 */
@Service("authorityPermissionService")
public class AuthorityPermissionServiceImpl implements IAuthorityPermissionService {

    
    @Autowired
    private AuthorityPermissionDao authorityPermissionDao;

	private final int QUERY_LIMLIT = 10000;

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public Long add(AuthorityPermission record) {
		record.setCreateTime(new Date());
		record.setVersionNum(1L);
        if(authorityPermissionDao.add(record)==1){
			return record.getId();
		}
		return 0L;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean edit(AuthorityPermission record) {
		record.setModifyTime(new Date());
		record.setVersionNum(authorityPermissionDao.get(record.getId()).getVersionNum());
        return authorityPermissionDao.update(record)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean delete(Long id) {

        return authorityPermissionDao.delete(id)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public AuthorityPermission get(Long id) {

        return authorityPermissionDao.get(id);
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public AuthorityPermissionQuery query(AuthorityPermissionQuery query) {
        int count =  authorityPermissionDao.count(query);
        query.setCount(count);
        query.setData(getTreePermissions(query));
        return query;
    }


    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public List<AuthorityPermission> getList(AuthorityPermissionQuery query) {
        int count = authorityPermissionDao.count(query);
        int pags = (count - 1) / QUERY_LIMLIT + 1;
        List<AuthorityPermission> authorityPermissionList = new ArrayList<>(count);
        for(int i = 1 ;i <= pags ; i++ ){
            query.setPage(i);
            query.setLimit(QUERY_LIMLIT);
            authorityPermissionList.addAll(authorityPermissionDao.query(query));
        }
        return authorityPermissionList;
    }

    /**
     * 获取树状结构的数据
     * @param query
     * @return
     */
    private List<AuthorityPermission> getTreePermissions(AuthorityPermissionQuery query) {
        List<AuthorityPermission> authorityPermissions = authorityPermissionDao.getList(query);
        if(StringUtil.isNotEmpty(authorityPermissions)){
            for(AuthorityPermission authorityPermission : authorityPermissions){
                if(StringUtil.isEmpty(authorityPermission.getParentCode())){
                    authorityPermission.setTreePid(0L);
                }else{
                    query.setPermissionCode(authorityPermission.getParentCode());
                    authorityPermission.setTreePid(authorityPermissionDao.getList(query).get(0).getId());
                }
            }
        }
        return authorityPermissions;
    }

    @Override
    public List<AuthorityPermission> getLevelList(AuthorityPermissionQuery query) {
        return getLevelPermission(query);
    }

    /**
     * 获取上下级结构的资源
     * @param query
     * @return
     */
    private List<AuthorityPermission> getLevelPermission(AuthorityPermissionQuery query) {
        query.setPermissionType(EnumPermissionType.MENU_GUOP.getCode());
        //获取菜单组数据
        List<AuthorityPermission> authorityPermissionList = authorityPermissionDao.getList(query);
        for(AuthorityPermission authorityPermission : authorityPermissionList){
            query.setPermissionType(EnumPermissionType.MENU.getCode());
            query.setParentCode(authorityPermission.getPermissionCode());
            //获取菜单数据
            authorityPermission.setChildPermissons(authorityPermissionDao.getList(query));

        }
        return authorityPermissionList;
    }
}
